package org.peterbjornx.gamestate;

import org.peterbjornx.Client;
import org.peterbjornx.util.TextUtils;
import org.peterbjornx.io.RSOutputStream;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.*;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: PeterbjornX
 * Date: Apr 9, 2009
 * Time: 2:50:32 PM
 * Defines a simple logged in behaviour
 */
public class SimpleGameState extends GameState{
    private RSOutputStream realoutstream;
    private ByteArrayOutputStream byteout;
    private RSOutputStream outstream;
    private static final int CHAT_H = 160;
    private static final int CHAT_W = 496;
    private static final int GAME_X = 10;
    private static final int GAME_Y = 4;
    private static final int GAME_W = 512;
    private static final int GAME_H = 334;
    private String[] messages = new String[6];
    private int mp = 0;
    private static final int INPUT_BOX = 1;

    private int inputstate;
    private String chat = "";
    private String box = "";

    private static final int INPUT_CHAT = 0;
    private static final int CHAT_PACKET = 4;

    public SimpleGameState(Client c, OutputStream _outstream, int[] isaacSeed) {
        super(c);
        for (int i = 0;i < isaacSeed.length;i++)
            isaacSeed[i]-=50;
        realoutstream = new RSOutputStream(_outstream);
        byteout = new ByteArrayOutputStream();
        outstream = new RSOutputStream(byteout);
        outstream.initISAACCypher(isaacSeed);
        myClient.packethandler.start();
    }

    public void paint(Graphics g){
        //g.setColor(Color.black);
        //g.fillRect(0,0,myClient.getWidth(),myClient.getHeight());
        g.setColor(Color.white);
        g.fillRect(GAME_X,GAME_Y+GAME_H,CHAT_W,CHAT_H);
        int y = GAME_Y+GAME_H+6+g.getFontMetrics().getHeight();
        g.setColor(Color.black);
        for (int i = 0;i < 6;i++){
            if (messages[i] != null)
                g.drawString(messages[i],GAME_X+5,y);
            y+=g.getFontMetrics().getHeight() + 3;
        }
        y+=g.getFontMetrics().getHeight() + 3;
        g.drawString(myClient.myplayer.name+": "+chat,GAME_X+5,y);
        //g.clipRect(GAME_X,GAME_Y,GAME_W,GAME_H);
        //if (myClient.mylandscape != null)
        //    myClient.landscaperenderer.renderlandscape(myClient.mylandscape,g,GAME_X,GAME_Y, 2*60, 2*64,GAME_W,GAME_H);
    }

    private void flush() throws IOException {
        outstream.flush();
        byte d[] = byteout.toByteArray();
        realoutstream.write(d);
        byteout = new ByteArrayOutputStream();
        outstream.set(byteout);
    }
    public void notifyloaded() {
        try {
            outstream.writeISAACByte(0);flush();
            needvp_change = true;
            vp_x = GAME_X;
            vp_y = CHAT_H;
            vp_w = GAME_W;
            vp_h = GAME_H;
            opengl_enabled = true;
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void addMessage(String s){
        if (mp == 6){
            String[] m = new String[6];
            System.arraycopy(messages,1,m,0,5);
            messages = m;
            mp = 5;
        }
        messages[mp++] = s;
        myClient.repaint();
    }

    public void keyTyped(KeyEvent e) {
        int ins = inputstate;
        String opw = box;
        String oun = chat;
        if (inputstate == INPUT_CHAT) {
            if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && chat.length() > 0 )
                chat = chat.substring(0, chat.length()-1);
          //  else if (e.getKeyCode() == KeyEvent.VK_TAB || e.getKeyChar() == '\t')
          //      inputstate = INPUT_PASSWORD;
            else if (e.getKeyChar() > 0 && e.getKeyChar() < 128 && e.getKeyChar() != '\t' && e.getKeyChar() != 8)
                chat +=e.getKeyChar();
        } else if (inputstate == INPUT_BOX){
            if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && box.length() > 0 )
                box = box.substring(0, chat.length()-1);
            //else if (e.getKeyCode() == KeyEvent.VK_TAB || e.getKeyChar() == '\t')
           //     inputstate = INPUT_USERNAME;
            else if (e.getKeyChar() > 0 && e.getKeyChar() < 128 && e.getKeyChar() != '\t' && e.getKeyChar() != 8)
                box +=e.getKeyChar();
        }
        if (!box.equals(opw) || !chat.equals(oun) || inputstate != ins) myClient.repaint();
    }

    public void keyPressed(KeyEvent e) {
        int ins = inputstate;
        String opw = box;
        String oun = chat;

        if (inputstate == INPUT_CHAT) {
            if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && chat.length() > 0 )
                chat = chat.substring(0, chat.length()-1);
            else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
                chat = "";
            else if (e.getKeyCode() == KeyEvent.VK_ENTER)
                chataction();
        } else if (inputstate == INPUT_BOX){
            if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && box.length() > 0 )
                box = box.substring(0, box.length()-1);
            else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
                box = "";
        }
        if (!box.equals(opw) || !chat.equals(oun) || inputstate != ins) myClient.repaint();
    }

    private void chataction() {
        try {
            if (chat == null || chat.equals(""))
                return;
            outstream.writeISAACByte(CHAT_PACKET);
            byte d[] = new byte[256];
            int l = TextUtils.textPack(d,chat);
            addMessage(myClient.myplayer.name+" says "+chat);
            chat = "";
            outstream.write(l+2);
            outstream.writeByte(128);
            outstream.writeByte(128);
            outstream.writeBytes_reverseA(d,0,l);
            flush();
        } catch (IOException e) {
            System.out.println("[Chat System] Exception while sending chat");
        }
    }

    public void keyReleased(KeyEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void mouseClicked(MouseEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void mousePressed(MouseEvent e) {
        myClient.pickPoint = e.getPoint();
        myClient.gl_pickrequest = true;
    }

    public void mouseReleased(MouseEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void mouseEntered(MouseEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void mouseExited(MouseEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}


